package com.example.demo.controller.ops;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.example.camera.CatD;
import com.example.demo.model.ResposeModel;

@Controller
@RequestMapping("/ops/fileupload/")
public class FileUploadOpsController {

	@RequestMapping(value="/upload", method = RequestMethod.POST)
    public @ResponseBody ResposeModel upload(@RequestParam("file") MultipartFile file,
            HttpServletRequest request) {
        String contentType = file.getContentType();
        
        String fileName=new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())+file.getOriginalFilename();
        //String fileName = file.getOriginalFilename();
        String filePath = request.getSession().getServletContext().getRealPath("/imgupload/");
        System.out.println(filePath);
        ResposeModel res = new ResposeModel();
        try {
            CatD.uploadFile(file.getBytes(), filePath, fileName);
            res.setData("/imgupload/"+fileName);
        } catch (Exception e) {
        	e.printStackTrace();
        	res.setStatus("0");
        }
        
		return res;        
    }
}
